package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.ChangeExecution;
import com.frcdi.qms.masterdata.service.IChangeExecutionService;
import com.frcdi.qms.system.domain.vo.DigitalSignVO;
import com.frcdi.qms.system.service.ISysDigitalSignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 变更执行Controller
 *
 * @author cell
 * @date 2023-09-25
 */
@Api(tags = "变更执行")
@RestController
@RequestMapping("/masterData/execution")
public class ChangeExecutionController extends BaseController
{
    @Autowired
    private IChangeExecutionService changeExecutionService;

    @Autowired
    private ISysDigitalSignService signService;

    /**
     * 查询变更执行列表
     */
    @ApiOperation("查询变更执行列表")
    @PreAuthorize("@ss.hasPermi('masterData:execution:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChangeExecution changeExecution)
    {
        startPage();
        List<ChangeExecution> list = changeExecutionService.selectChangeExecutionList(changeExecution);
        return getDataTable(list);
    }

    /**
     * 导出变更执行列表
     */
    @ApiOperation("导出变更执行列表")
    @PreAuthorize("@ss.hasPermi('masterData:execution:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChangeExecution changeExecution)
    {
        List<ChangeExecution> list = changeExecutionService.selectChangeExecutionList(changeExecution);
        ExcelUtil<ChangeExecution> util = new ExcelUtil<ChangeExecution>(ChangeExecution.class);
        util.exportExcel(response, list, "变更执行数据");
    }

    /**
     * 获取变更执行详细信息
     */
    @ApiOperation("获取变更执行详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:execution:query')")
    @GetMapping(value = "/{executionId}")
    public AjaxResult getInfo(@PathVariable("executionId") Long executionId)
    {
        return success(changeExecutionService.selectChangeExecutionByExecutionId(executionId));
    }

    /**
     * 新增变更执行
     */
    @ApiOperation("新增变更执行")
    @PreAuthorize("@ss.hasPermi('masterData:execution:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ChangeExecution changeExecution)
    {
        changeExecution.setCreateBy(getUsername());
        return toAjax(changeExecutionService.insertChangeExecution(changeExecution));
    }

    /**
     * 修改变更执行
     */
    @ApiOperation("修改变更执行")
    @PreAuthorize("@ss.hasPermi('masterData:execution:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ChangeExecution changeExecution)
    {
        if (StringUtils.isNotEmpty(changeExecution.getExecutionBy())){
            changeExecution.setExecutionTime(DateUtils.getNowDate());
        }
        changeExecution.setUpdateBy(getUsername());
        return toAjax(changeExecutionService.updateChangeExecution(changeExecution));
    }

    /**
     * 删除变更执行
     */
    @ApiOperation("删除变更执行")
    @PreAuthorize("@ss.hasPermi('masterData:execution:remove')")
	@DeleteMapping("/{executionIds}")
    public AjaxResult remove(@PathVariable Long[] executionIds)
    {
        return toAjax(changeExecutionService.deleteChangeExecutionByExecutionIds(executionIds));
    }

    /**
     * 数字签名
     */
    @ApiOperation("数字签名")
    @PostMapping("/digitalSign")
    public AjaxResult digitalSign(@RequestBody DigitalSignVO signVo)
    {
        return toAjax(signService.digitalSign(signVo));
    }
}
